package com.ham.mud.commands;

import com.ham.mud.Runner;
import com.ham.mud.ServerConnection;
import com.ham.mud.characters.player.Player;
import com.ham.mud.classes.MudClass;

import java.util.List;

/**
 * Created by hlucas on Jun 29, 2011 at 3:38:09 PM
 */
public class WhoCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        List<ServerConnection> connections = Runner.STATIC_INSTANCE.getConnections();
        connection.printStart("Who is online:");
        connection.println(" --------------------------------------");
        connection.println("|   NAME" + getSpaces("name") + "CLASS" + getSpaces("class") + "LEVEL" + getSpaces("level"));

        connection.println(" --------------------------------------");
        for(ServerConnection conn : connections) {
            Player player = conn.getPlayer();
            MudClass mudClass = player.getMudClass();
            int level = player.getLevel();
            connection.println("|   " + player.getName() + getSpaces(player.getName()) + mudClass.getName() + getSpaces(mudClass.getName()) + level + getSpaces(String.valueOf(level)));
        }
        connection.println(" --------------------------------------");
        connection.println(" Player(s) online: " + connections.size());
        connection.printEnd();
    }

    private String getSpaces(String name) {
        int num = 12 - name.length();
        int atBar = 8 - name.length();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < num; i++) {
            sb.append(" ");
            if(i == atBar) {
                sb.append("|");
            }
        }
        return sb.toString();
    }

    @Override
    protected String getCommandName() {
        return "who";
    }
}
